﻿using System;
using Wicresoft.Framework.Authentication;
using System.Web.UI;
using Wicresoft.Common;
using Attendences.Framework.Entity;
using Wicresoft.Framework.Organization;
using System.Collections.Generic;
using System.Net;

public partial class Page_SignAttend : Page
{
    private string DomainPrefix
    {
        get
        {
            Domain defaultDomain = AuthenticationHelper.GetDefaultDomain();
            if (defaultDomain != null)
            {
                return (defaultDomain.Name + @"\");
            }
            return string.Empty;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            AttendenceSign att = new AttendenceSign();
            if (att.Load(string.Format("{0}='{1}' and {2}>='{3}' and {2}<='{4}'", AttendenceSign.SQLCOL_SIGNUSER, tbUser.Text.Replace("'", "''"), AttendenceSign.SQLCOL_SIGNTIME, DateTime.Today.ToString("yyyy-MM-dd 0:0:0"), DateTime.Today.AddDays(1).ToString("yyyy-MM-dd 0:0:0"))))
            {
                btnSige.Visible = false;
                ltlSign.Text = "今天您已签到";
            }
        }
    }
    protected void btnSige_Click(object sender, EventArgs e)
    {
        Guid userID = Guid.Empty;

        Wicresoft.Framework.Organization.User ur = Wicresoft.Framework.Organization.OrganizationHelper.GetUserInfo(tbUser.Text.Trim());

        string username = string.Empty;
        string userdspname = string.Empty;
        Guid deptid = Guid.Empty;
        string deptname = string.Empty;

        if (ur == null)
        {
            OtherUser ou = new OtherUser();
            if (ou.Load(string.Format("(name='{0}' or displayname='{0}') and pwd ='{1}'", tbUser.Text, Helper.MD5Hash(tbPwd.Text))))
            {
                userID = ou.ID;
                username = ou.Name;
                userdspname = ou.DisplayName;


            }

        }

        else
        {
            wsafauth.AuthenticationService auth = new wsafauth.AuthenticationService();
            ICredentials tIC = new NetworkCredential("admin", "Password01!", "jd");
            auth.Credentials = tIC;
            userID = auth.CheckUserPassword(this.DomainPrefix + tbUser.Text.Trim(), tbPwd.Text);
            if (userID != Guid.Empty)
            {
                userID = ur.ID;

                username = ur.Name;
                userdspname = ur.DisplayName;

                deptid = ur.MainDepartmentID;
                deptname = ur.GetMainDepartment().DisplayName;


                Department[] deptList = ur.GetDepartments();
                List<Department> otherList = new List<Department>();
                foreach (Department item in deptList)
                {
                    if (item.DisplayName != "嘉定新城（马陆镇）")
                    {
                        otherList.Add(item);
                    }
                }
                if (otherList.Count > 0)
                {
                    deptid = otherList[0].ID;
                    deptname = otherList[0].DisplayName;

                }
            }

        }
        if (userID != Guid.Empty)
        {
            AttendenceSign att = new AttendenceSign();
            if (!att.Load(string.Format("{0}='{1}' and {2}>='{3}' and {2}<='{4}'", AttendenceSign.SQLCOL_SIGNUSER, username, AttendenceSign.SQLCOL_SIGNTIME, DateTime.Today.ToString("yyyy-MM-dd 0:0:0"), DateTime.Today.AddDays(1).ToString("yyyy-MM-dd 0:0:0"))))
            {


                att = new AttendenceSign();
                att.SignDisplayName = userdspname;
                att.SignUser = username;
                att.SignTime = DateTime.Now;
                att.DepartmentID = deptid;
                att.DepartmentDisplayName = deptname;
                att.Add();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "<script>alert('签到成功');</script>");

            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "<script>alert('已签到');</script>");

            }
            int count = 0;
            rpList.DataSource = DataAccess.Select(typeof(AttendenceSign), 0, 5, "SignUser='" + username + "'", "SignTime desc", ref count);
            rpList.DataBind();
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "<script>alert('用户密码错误');location=location;</script>");
        }
    }
}
